home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cenvid.zip / CMM_VS_C.DOC < prev    next >
Text File  |  1993-08-10  |  21KB  |  477 lines

  1.                      CEnvi Shareware Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  3.  
  4.  
  5.                       CEnvi unregistered version 1.0
  6.                                9 AUGUST 1993
  7.  
  8.                        CEnvi Shareware User's Manual
  9.  
  10.           Copyright 1993, Nombas, All Rights Reserved.
  11.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  12.           (617)391-6595, (617)391-5289
  13.  
  14.  
  15.           Thank you for trying this shareware version of CEnvi from Nombas.
  16.  
  17. 3.  Cmm versus C: The Cmm language for C programmers
  18.  
  19.           This chapter is for those who already know how to program in the
  20.           C language.  This chapter describes only those elements of Cmm
  21.           that differ from standard C, and so if you don't already
  22.           understand C, then this shouldn't have much meaning for you.
  23.           Non-C programmers should instead look at the previous chapter.
  24.  
  25.           Since it is assumed that readers of this chapter are already
  26.           knowledgeable in C, only those aspects of Cmm that differ from C
  27.           are described here.  If it's not mentioned here, then assume that
  28.           Cmm behavior will be standard C.
  29.  
  30.           Deviations from C are a result of these two harmonious Cmm
  31.           directives: Convenience and Safety.  Cmm is different from C
  32.           where the change makes Cmm more convenient for small programs,
  33.           command-line code, or scripting files, or if unaltered C rules
  34.           encourage coding that is potentially unsafe.
  35.  
  36. 3.1.  C Minus Minus
  37.  
  38.           Cmm is "C minus minus" where the minuses are Type Declarations
  39.           and Pointers.  If you already know C and can remember to forget
  40.           these two aspects of C (I repeat, no Type Declarations and no
  41.           Pointers) then you know Cmm.  If you were to take C code, and
  42.           delete all the lines, code-words, and symbols that either declare
  43.           data types or explicitly point to data, then you would be left
  44.           with Cmm code; and although you would be removing bytes of source
  45.           code, you would not be removing capabilities.
  46.  
  47.           All of the details below that compare Cmm against C follow from
  48.           the general rule:
  49.             *Cmm is C minus Type Declarations and minus Pointers.
  50.  
  51. 3.2.  Data Types
  52.  
  53.           The only recognized data types are Float, Long, and Byte.  The
  54.           words "Float", "Long", and "Byte" do not appear in Cmm source
  55.           code; instead, the data types are determined by context.  For
  56.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  57.           unsigned, and the other types are signed.
  58.  
  59. 3.3.  Automatic Type Declaration
  60.  
  61.           There are no type declarators and no type casting.  Types are
  62.           determined from context.  If the code says "i=6" then i is a
  63.           Long, unless a previous statement has indicated otherwise.
  64.  
  65.           For instance, this C code:
  66.               int Max(int a,int b)
  67.               {
  68.                 int result;
  69.                 result = ( a < b ) ? b : a ;
  70.                 return result;
  71.               }
  72.           could become this Cmm code:
  73.               Max(a,b)
  74.               {
  75.                 result = ( a < b ) ? b : a ;
  76.                 return result;
  77.               }
  78.  
  79. 3.4.  Array Representation
  80.  
  81.           Arrays are used in Cmm much like they are in C, except that they
  82.           are stored differently: a first-order array (e.g., a string) is
  83.           stored in consecutive bytes in memory, but arrays of arrays are
  84.           not in consecutive memory locations.  The C declaration "char
  85.           c[3][3];" would state that there are sixteen consecutive bytes in
  86.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  87.           tell you that there are (at least) three arrays of characters,
  88.           and the third array of arrays has (at least) three characters in
  89.           it; and although the characters in c[0] are in consecutive bytes,
  90.           and the characters in c[1] are in consecutive bytes, the two
  91.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  92.  
  93. 3.4.1   Array Arithmetic
  94.  
  95.           When one array is assigned to the other, as in:
  96.               foo = "cat";
  97.               goo = foo;
  98.           then both variables define the same array and start at the same
  99.           offset 0.  In this case, if foo[2] is changed then you will find
  100.           that goo[2] has also been changed.
  101.  
  102.           Integer addition and subtraction can also be performed on arrays.
  103.           Array addition or subtraction sets where the array is based.  By
  104.           altering the previous code segment to:
  105.               foo = "cat";
  106.               goo = foo + 1;
  107.           goo and foo would now be arrays containing the same data, except
  108.           that now goo is based one element further, and foo[2] is now the
  109.           same data as goo[1].
  110.  
  111.           To demonstrate:
  112.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  113.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  114.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  115.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  116.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  117.  
  118. 3.4.2   Automatic Array Allocation
  119.  
  120.           Arrays are dynamic, and any index, (positive or negative) into an
  121.           array is always valid.  If an element of an array is referred to,
  122.           then the Cmm must see to it that such an element will exist.  For
  123.           instance if the first statement in the Cmm source code is "foo[4]
  124.           = 7;" then the Cmm interpreter will make an array of 5 integers
  125.           referred to by the variable foo.  If a statement further on
  126.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  127.           has to, to ensure that the element foo[6] exists.  This works
  128.           with negative indices as well.  When you refer to foo[-10], then
  129.           foo is grown in the other direction if it needs to be, but foo[4]
  130.           will still refer to that "7" you put there earlier.  Arrays can
  131.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  132.           valid.
  133.  
  134. 3.5.  Structures
  135.  
  136.           Structures are created dynamically, and their elements are not
  137.           necessarily contiguous in memory.  When CEnvi comes across the
  138.           statement 'foo.animal = "dog"' it creates a structure element of
  139.           foo that is referred to as "animal" and is an array of
  140.           characters, and this "animal" variable is thereafter carried
  141.           around with "foo" (much like a stem variable in REXX).  The
  142.           resulting code looks very much like regular C code, except that
  143.           there is not a separate structure definition anywhere.
  144.  
  145.           This C code:
  146.  
  147.               struct Point {
  148.                 int Row;
  149.                 int Column;
  150.               };
  151.  
  152.               struct Square {
  153.                 struct Point BottomLeft;
  154.                 struct Point TopRight;
  155.               };
  156.  
  157.               void main()
  158.               {
  159.                 struct Square sq;
  160.                 int Area;
  161.                 sq.BottomLeft.Row = 1;
  162.                 sq.BottomLeft.Column = 15;
  163.                 sq.TopRight.Row = 82;
  164.                 sq.TopRight.Column = 120;
  165.                 Area = AreaOfASquare(sq);
  166.               }
  167.  
  168.               int AreaOfASquare(struct Square s)
  169.               {
  170.                 int width, height;
  171.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  172.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  173.                 return( length * height );
  174.               }
  175.  
  176.           can be changed into the equivalent Cmm code simply be removing
  177.           declaration lines, resulting in:
  178.  
  179.               main()
  180.               {
  181.                 sq.BottomLeft.Row = 1;
  182.                 sq.BottomLeft.Column = 15;
  183.                 sq.TopRight.Row = 82;
  184.                 sq.TopRight.Column = 120;
  185.                 Area = AreaOfASquare(sq);
  186.               }
  187.  
  188.               int AreaOfASquare(s)
  189.               {
  190.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  191.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  192.                 return( length * height );
  193.               }
  194.  
  195.           Structures can be passed, returned, and modified just as any
  196.           other variable.  Of course structures and arrays are independent,
  197.           so you could very well have the statement "foo[8].animal.forge[3]
  198.           = bil.bo".
  199.  
  200. 3.6.  Passing Variables by Reference
  201.  
  202.           By default, LValues in Cmm are passed to functions by reference,
  203.           and so if the function alters a variable then the variable in the
  204.           calling function is altered as well IF IT IS AN LVALUE.  So
  205.           instead of this C code:
  206.  
  207.               main() {
  208.                 .
  209.                 .
  210.                 .
  211.                 CQuadrupleInPlace(&i);
  212.                 .
  213.                 .
  214.                 .
  215.               }
  216.  
  217.               void CQuadrupleInPlace(int *j)
  218.               {
  219.                 *j *= 4;
  220.               }
  221.  
  222.           the Cmm version would be:
  223.  
  224.               main() {
  225.                 .
  226.                 .
  227.                 .
  228.                 QuadrupleInPlace(i);
  229.                 .
  230.                 .
  231.                 .
  232.               }
  233.  
  234.               void QuadrupleInPlace((j)
  235.               {
  236.                 j *= 4;
  237.               }
  238.  
  239.           If the rare circumstance arises that you want to pass a copy of
  240.           an LValue to a function, instead of passing the variable by
  241.           reference, then you can use the Cmm "copy-of" operator "=".
  242.           foo(=i) can be interpreted as saying "pass a value equal to i,
  243.           but not i itself"; so that "foo(=i) ... foo(j) { j *= 4; }" would
  244.           not change the value at i.
  245.  
  246.           Note that for this Cmm version, the following calls to
  247.           QuadrupleInPlace() would be valid, but no value will have changed
  248.           upon return from QuadrupleInPlace() because an LValue is not
  249.           being passed:
  250.               QuadrupleInPlace(8);
  251.               QuadrupleInPlace(i+1);
  252.               QuadrupleInPlace(=1);
  253.  
  254. 3.7.  Data Pointers(*) and Addresses(&)
  255.  
  256.           No pointers.  None.  The "*" symbol NEVER means "pointer" and the
  257.           "&" symbol never means "address".  This may at first cause
  258.           seasoned C programmers to gasp in disbelief, but it turns out to
  259.           be not such a big deal, and these two operators are seldom
  260.           missed, after considering these two rules:
  261.               1) "*var" can be replaced in all instances by "var[0]"
  262.               2) variables (if LValues) are passed by reference
  263.  
  264. 3.8.  Case Statements
  265.  
  266.           Case statements in a switch statement may be a constant, a
  267.           variable, or any other statement that can be evaluated to a
  268.           variable.  So you might see this Cmm code:
  269.  
  270.               switch(i) {
  271.                 case 4:
  272.                 case foe():
  273.                 case sqrt(foe()):
  274.                 case (PILLBOX * 3 - 2):
  275.                 default:
  276.               }
  277.  
  278. 3.9.  Initialization: Code external to functions
  279.  
  280.           All code that is not inside any function block is interpreted
  281.           before main() is called.  So the following Cmm code:
  282.  
  283.               printf("hey there ");
  284.  
  285.               main()
  286.               {
  287.                 printf("ho there ");
  288.               }
  289.  
  290.               printf("hi there ");
  291.  
  292.           would result in the output "hey there hi there ho there ".
  293.  
  294. 3.10. Unnecessary tokens
  295.  
  296.           If symbols are redundant, then they are usually unnecessary in
  297.           Cmm.  This allows for more flexibility in the non-C-trained and
  298.           also lets more code get in the tiny space available on the
  299.           command line.  Besides, I got tired of my compiler saying
  300.           "missing semi-colon"; What good is a semi-colon if it doesn't
  301.           tell the compiler anything new?  So you can have the statement
  302.           "foo()" as well as "foo();".  It certainly doesn't hurt to have
  303.           the semi-colon there, especially when it can clarify a "return;"
  304.           statement, for example, but it isn't necessary.  Similarly, "("
  305.           and ")" are often unnecessary, so you may have "while a < b a++"
  306.           as a complete statement.
  307.  
  308. 3.11. #include
  309.  
  310.           The #include statement has been enhanced for reading source
  311.           snippets from within other types of files.  So we have
  312.  
  313.               #Include <filespec,Extension,Prefix,HeaderLine,FooterLine>
  314.  
  315.           where filespec is the same as in C's #include <filespec>
  316.           statement, Extension is a file extension (such as BAT) that may
  317.           be added to the filespec (so batch files can say #include
  318.           <%0,bat>", Prefix specifies that only those lines in filespec
  319.           that begin with Prefix will be source, and HeaderLine and
  320.           FooterLine specify that source will be read only from sections of
  321.           filespec between HeaderLine and FooterLine.  If a full path is
  322.           not specified then CEnvi searches for the file in various paths
  323.           in this order:
  324.             *Search the current directory.
  325.             *If the code is run from a *.cmm source file, then search in
  326.               the source directory for the *.cmm file.
  327.             *If this is the Windows version of CEnvi, searches all the
  328.               files in the CMMPATH profile value (from WIN.INI in the
  329.               [CEnvi] section).
  330.             *Search all directories in the CMMPATH environment variable.
  331.             *Search the directory that CEnvi.exe is executed from.
  332.             *Search all directories from the PATH environment variable.
  333.  
  334. 3.12. Macros
  335.  
  336.           Function macros are not supported.  Since speed is not of primary
  337.           importance, a macro gains little over a function call, and so any
  338.           macros may simply become functions.
  339.  
  340.           Token replacement macros ("#define NULL 0") are supported in Cmm.
  341.  
  342. 3.13. Converting existing C code to Cmm
  343.  
  344.           Converting existing C code to Cmm, should you choose to do so, is
  345.           mostly a process of deleting unnecessary text.  You search on
  346.           type declarations, such as "int", "float", "struct", "char",
  347.           "[]", etc... and then delete these declaration strings.  For
  348.           instance, these instances of C code (or C++ code) on the left can
  349.           be replaced by the Cmm code on the right:
  350.  
  351.                    C                               Cmm     
  352.               ----------                      -------------
  353.  
  354.               int i;    ....................  i (or nothing at all)
  355.  
  356.               int foo = 3; .................. foo = 3;
  357.  
  358.               struct {    ................... /* nothing */
  359.                 int row;
  360.                 int col;
  361.               }
  362.  
  363.               char name[] = "George"; ....... name = "George";
  364.  
  365.               int goo(int a,char *s,int &c) . goo(a,s,c)
  366.  
  367.               int zoo[] = { 1, 2, 3 }; ...... zoo = { 1, 2, 3 };
  368.  
  369.           The next step in converting C to Cmm is to search for the address
  370.           and pointer operators ("*", "&").  If the '&' and '*' are
  371.           together so that the address of a variable is passed to a
  372.           function, then both of these operators become unnecessary because
  373.           Cmm passes lvars by reference.  If there are still "*" found then
  374.           they are usually referring to the zeroth value of a pointer
  375.           address, and so can be replaced with [0], as in "*foo = 4"
  376.           replaced by "foo[0] = 4".  Finally, the "->" operator for
  377.           structures must be replaced by "." either because the structure
  378.           is now being passed by referenced or because the element of the
  379.           structure is being referred to by its array index: "foo->row" may
  380.           need to become "foo[0].row".
  381.  
  382. -------------------------------- FILE LIST --------------------------------
  383. The CEnvi Unregistered Shareware package includes the files in the
  384. following list.  You are not permitted to upload or otherwise transfer
  385. copies of any registered version of CEnvi that does not include all of the
  386. files in this list.
  387.  
  388. *CENVI.EXE: CEnvi shareware executable for DOS, OS/2, or Windows.
  389. *CENVI.DOC: CEnvi Shareware Manual, Chapter 1: CEnvi Unregistered Shareware
  390. *CMMTUTOR.DOC: CEnvi Shareware Manual, Chapter 2: Cmm Language Tutorial
  391. *CMM_VS_C.DOC: CEnvi Shareware Manual, Chapter 3: Cmm versus C, for C
  392.   Programmers
  393. *CENVILIB.DOC: CEnvi Shareware Manual, Chapter 4: Function Library
  394. *FILELIST.DOC: This file.
  395. *LICENSE.DOC: CEnvi Unregistered Shareware License Agreement
  396. *REGISTER.DOC: CEnvi registration form
  397. *INSTALL.CMM: Cmm source file for installing this shareware version
  398. * *.CMM, *.CMD, *.BAT, *.LIB: Many many sample programs using CEnvi.  See
  399.   CENVI.DOC for a complete list.
  400.  
  401. ----------------------------- REGISTRATION -------------------------------
  402. This is a shareware release.  Please register.  As a registered CEnvi user
  403. you will receive:
  404. *The latest version of CEnvi for all supported platforms (currently DOS,
  405.   OS/2, and Windows).
  406. *The CEnvi user's manual (almost 100 pages, including a description of the
  407.   Cmm programming language, a tutorial for those who have never programmed,
  408.   and descriptions and examples of the nearly 150 functions included in the
  409.   CEnvi library).
  410. *Free incremental electronic downloads for new versions of CEnvi for all
  411.   supported operating systems.
  412. *Unlimited support from Nombas and CEnvi/Cmm users through CompuServe
  413.   (72212,1622), internet (bsn@world.std.com), the cenvi-cmm e-mail mailing
  414.   list (cenvi-cmm@world.std.com), and the Nombas BBS (617-391-6595).
  415. *Access to the growing list of CEnvi utilities and libraries (some of which
  416.   are included in this unregistered shareware package, and others are
  417.   contributed by Nombas and CEnvi/Cmm users to the electronic locations
  418.   described above).
  419.  
  420. ------------------------- CENVI REGISTRATION FORM -------------------------
  421. Thank you for registering your shareware copy of CEnvi version 1.0.  Please
  422. fill out and mail in this form, along with payment.
  423.  
  424. Where did you get CEnvi? ______________________________________________
  425.  
  426. Name: _________________________________________________________________
  427.  
  428. Company: ______________________________ Position: _____________________
  429.  
  430. Address: ______________________________________________________________
  431.  
  432. _______________________________________________________________________
  433.  
  434. ______________________________________________________________________
  435.  
  436. Country: _________________________   (add ZIPcode if applicable)
  437.  
  438. Phone: ___________________________  EMail: ______________________________
  439.  
  440.           Diskette size: [  ] 3.5"   [  ] 5.25"
  441.  
  442. CEnvi Registered License ............ Quantity _____ x $38.00 = $ _________
  443. Additional CEnvi Registered licenses for your
  444. organization (does not include additional manuals
  445. or diskettes).  You must already be a registered
  446. user to purchase additional licenses.
  447.                     Additonal License Quantity _____ x $15.00 = $ _________
  448. Additional CEnvi Manuals.  You must already be a 
  449. registered user to purchase additional manuals.
  450.                    Additional Manuals Quantity _____ x $10.00 = $ _________
  451. Friend-of-Cmm Discount: enter $-5.00, and BBS
  452. location, if you have uploaded CEnvi Unregistered
  453. Shareware to a BBS where it wasn't already available.
  454.     BBS Location ____________________________________________   $-_________
  455. Shipping outside USA, Canada, or Mexico  $4.00 ................ $ _________
  456.                                                        Subtotal $ _________
  457. Massachusetts residents please add 5% sales tax ............... $ _________
  458.  
  459.                                                           Total $ _________
  460.  
  461. Include a check or money order for this total, in U.S. funds and drawn on a
  462. U.S. bank, payable to Nombas.  Mail with this form to:
  463.                Nombas
  464.                P.O. Box 875
  465.                Medford, MA  02155   USA
  466.  
  467. Nombas may also be contacted at:
  468.      BBS: (617)391-6595
  469.      Phone: (617)391-6595, (617)391-5289
  470.      Internet: bsn@world.std.com
  471.      CompuServe: 72212,1622
  472.  
  473.  
  474. Nombas welcomes all comments, good or bad, regarding CEnvi and Cmm.  Please
  475. use the back of this form to give us all your suggestions about CEnvi and
  476. Cmm.  This is version 1.0; Where should we go from here?
  477.